﻿using System;
using System.Windows.Forms;
using System.Xml;
using RibbonEditor.AppCode;
using RibbonEditor.CommandUIDefinitionClasses;

namespace RibbonEditor.Templates
{
    public class RibbonTemplates : ITemplate, IXmlBuildable, IChildDeletable
    {
        public string Id { get; set; }
        public GroupTemplate GroupTemplate { get; set; }

        public RibbonTemplates()
        {}

        public RibbonTemplates(XmlNode node):this()
        {
            try
            {
                this.Id = XmlHelper.GetAttribute(node, "Id"); ;

                if (node.SelectSingleNode("GroupTemplate") != null)
                {
                    this.GroupTemplate = new GroupTemplate(node.SelectSingleNode("GroupTemplate"));
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode templatesNode)
        {
            try
            {
                XmlNode ribbonTemplatesNode = XmlHelper.AddNode(templatesNode, "RibbonTemplates");
                XmlHelper.AddAttribute(ribbonTemplatesNode, "Id", this.Id);

                if (this.GroupTemplate != null)
                {
                    this.GroupTemplate.AddNode(ribbonTemplatesNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode ribbonTemplatesNode = new TreeNode("RibbonTemplates");
                ribbonTemplatesNode.Tag = this;
                ribbonTemplatesNode.ContextMenuStrip = this.BuildContextMenu(ribbonTemplatesNode);

                if (this.GroupTemplate != null)
                {
                    this.GroupTemplate.BuildTreeViewElement(ribbonTemplatesNode);
                }

                parentNode.Nodes.Add(ribbonTemplatesNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode relatedNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Tag = relatedNode;

            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Group Template");
            tsmi.Click += new System.EventHandler(tsmi_Click);
            tsmi.Enabled = this.GroupTemplate == null;
            cms.Items.Add(tsmi);

            ContextMenuStripHelper.AddDisplayXmlMenu(cms, true);

            return cms;
        }

        void tsmi_Click(object sender, System.EventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

                this.GroupTemplate = new GroupTemplate();
                this.GroupTemplate.BuildTreeViewElement(selectedNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            if (o is GroupTemplate)
            {
                this.GroupTemplate = null;
            }
        }

        #endregion
    }
}
